package com.cloud.sso.common.log.factory;


import com.cloud.sso.common.util.SnowFlakeUtil;
import com.cloud.sso.entity.SysLog;
import com.cloud.sso.mapper.LoggerMapper;
import com.cloud.sso.oauth.obj.AppUserDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.thymeleaf.spring5.context.SpringContextUtils;

import java.util.Date;
import java.util.TimerTask;

/**
 * Created by tyd on 2017-10-13.
 * <p>
 * 日志任务工厂
 * 用来执行日志添加操作
 *
 * @author tyd
 */
@Slf4j
@Component
public class LogTaskFactory {

    @Autowired
    private LoggerMapper loggerMapper;

    public TimerTask log(final String content,
                         final String moduleName,
                         final String operateUser,
                         final String opType,
                         final String bId,
                         final String ip,
                         final String uri) {
        return new TimerTask() {
            @Override
            public void run() {
                SysLog entity = new SysLog(new SnowFlakeUtil().nextStringId(), moduleName, operateUser, opType, bId, content, new Date(), ip, uri);
                //记录日志信息
                loggerMapper.insert(entity);
                log.info(operateUser);
            }
        };
    }
}
